/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jtmcraft.game.communication.server;

import java.rmi.AccessException;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Collection;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import jtmcraft.game.communication.client.ClientInterface;

/**
 *
 * @author oem
 */
public class JTMCraftServer implements ServerInterface, Runnable {

    public void joinLocal(ClientInterface clientInterface) throws RemoteException {
        throw new UnsupportedOperationException("Not supported yet.");
    }


    public void sendMessage(int id, String message) throws RemoteException {
        Client client = (Client) clients.get( id );
        if( client != null )
        {
            sendAllMessage( client.name + ": " + message);
        }
    }

    private static class Client
    {
        
        public static int lastID = 1;
        
        public String name;
        public String host;
        public int port;
        public int ID;
       
        
        private ClientInterface stub;
        
        public Client(String name, String host, int port) {
            this.name = name;
            this.host = host;
            this.port = port;
            
            this.ID = JTMCraftServer.Client.lastID++;

            try {
                Registry registry = LocateRegistry.getRegistry(host, port);
                stub = (ClientInterface) registry.lookup(name);
            } catch (NotBoundException ex) {
                Logger.getLogger(JTMCraftServer.class.getName()).log(Level.SEVERE, null, ex);
            } catch (AccessException ex) {
                Logger.getLogger(JTMCraftServer.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RemoteException ex) {
                Logger.getLogger(JTMCraftServer.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }

        public ClientInterface getStub() {
            return stub;
        }

        public int getID() {
            return ID;
        }
                
        
    }
    
    static final public String name = "JTMCraftServer";
    private boolean working;
    private int port;
    
    private HashMap< Integer, Client> clients = new HashMap< Integer, Client>();

    public JTMCraftServer(int port) {
        this.port = port;

    }

    public int join(String name, String host, int port) throws RemoteException {
        System.out.println("Dołączam: " + name);
        Client client = new Client(name, host, port);
        clients.put(client.getID(), client);
        sendAllMessage( "Dołączył: " + name + " " + host + ":" + port);
        
        return client.getID();
    }

    public void stop() {
        working = false;
    }

    public void sendAllMessage( String msg )
    {
        Collection< Client > all = clients.values();
        for( Client client : all )
        {
            try {
                client.getStub().notifyTextMessage( msg );
            } catch (RemoteException ex) {
                Logger.getLogger(JTMCraftServer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public void run() {
        working = true;

        while (working) {
            try {
                //sendAllMessage( " Uwaga!!! to żyje " );
                Thread.sleep(500);
            } catch (InterruptedException ex) {
                Logger.getLogger(JTMCraftServer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public boolean isWorking() {
        return working;
    }
    
    
}
